<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Hash;

class CommentController extends Controller
{


    //后台评价列表
    public function getList(Request $request)
    {   
        $comments=DB::table('comments')->
            orderBy('id','desc')->
            where(function($query)use($request){
                $keyword=$request->input('keyword');
                if($keyword){
                    $query->where('name','like','%'.$keyword.'%');
                }
            })
            ->paginate($request->input('num',10));
        return view('admin.comment.list',['comments'=>$comments,'request'=>$request]);
    }
    //打开数据添加页面
    public function getEdit(Request $request)
    {   
        $id=$request->input('id');
        $comments=DB::table('comments')->where('id',$id)->first();
        return view('admin.comment.edit',['comments'=>$comments]);
    }


    //评价修改
    public function postUpdate(Request $request)
    {
        
        //获取提交值
        $data=$request->except(['_token','id']);
        // dd($data);
        //数据写入数据库
        $res=DB::table('comments')->where('id',$request->input('id'))->update($data);
        if($res){
            return redirect('comment/list')->with('info','修改成功');
        }else{
            return back()->with('error','修改失败');
        }
    }

    //评价删除
    public function getDrop(Request $request) 
    {
        $id=$request->all();
        // dd($id);
        $res=DB::table('comments')->where('id',$id)->delete();
        if($res){
            return redirect('comment/list')->with('info','删除成功');
        }else{
            return back()->with('error','删除失败');
        }
    }

    //添加评价
    public function getAdd()
    {
        return view('admin.comment.add');
    }

    //添加评价插入
    public function postInsert(Request $request)
    {
        $data=$request->except('_token');
        $id=$data['id'];
        // dd($id);
        //获取所要插入的评价内容
        $comment=$request->only('comment');
        // dd($comment);
        // dd($id);
        $res=DB::table('comments')->where('id','=',$id)->first();
        // dd($res);
        if($res){
            $res2=DB::table('comments')->where('id',$id)->update($comment);
            return redirect('comment/list')->with('info','更新成功');
        }else{
            return back()->with('info','商品不存在,不能添加评价');
        }
    }
    
    
}
